<?php
class datedropdown extends htmlwidget {
	public $year;
	public $month;
	public $day;
	
	public $yearwidget;
	public $monthwidget;
	public $daywidget;
	
	
	public $yeartitle = 'Year';
	public $monthtitle = 'Month';
	public $daytitle = 'Day';
	
	public $yearname = 'year';
	public $monthname = 'month';
	public $dayname = 'day';
	
	public $layout = 'vertical';//by default titles go above the drop down
	public $prefix = '';	
	public $suffix = '';
	public $randomstring = '';
	
	public $usepresetvalues = true;
	
	public function __construct( $day = '', $month = '',  $year = ''){
		if ( empty($year)) $year = date('Y');
		if ( empty($month)) $month = date('n');
		if ( empty($day)) $day = date('j');
		
		if (! checkdate ( $month, $day, $year ) ){
			trigger_error('Invalid date: ' . $month . '/' . $day . '/' . $year);
		}
		
		$this->yearwidget  = new yearwidget($year);
		$this->monthwidget = new monthwidget($month);
		$this->daywidget   = new daywidget($day);
		$this->randomstring = $this->randomstring(5);
	}
	
	public function usepreset($value=true){
		if ( $value ){
			$this->usepresetvalues = true;
		} else {
			$this->usepresetvalues = false;
		}
		if ( $this->usepresetvalues ){
			$this->daywidget->usepreset(true);
			$this->monthwidget->usepreset(true);
			$this->yearwidget->usepreset(true);
		} else {
			$this->daywidget->usepreset(false);
			$this->monthwidget->usepreset(false);
			$this->yearwidget->usepreset(false);			
		}
	}
	
	public function setlayout($layout){
		$layouts = array ('vertical', 'horizontal');
		$layout = strtolower(trim($layout));
		if ( in_array($layout, $layouts)){
			$this->layout = $layout;
		} else {
			trigger_error("Invalid layout: {$layout}");
		}
	}
	
	public function __toString(){
		$this->setnames();
		$this->setCaptions();
		if ( $this->layout == 'vertical'){
			return $this->printvertical();
		} else {
			return $this->printhorizontal();
		}
	}
	
	public function printvertical(){
		$table =  new table();
		$table->row($this->monthtitle, $this->daytitle, $this->yeartitle);
		$table->row($this->monthwidget, $this->daywidget, $this->yearwidget);
		return $table->getHtml();
	}
	
	public function printhorizontal(){
		$table =  new table();
		$table->row($this->monthtitle, $this->monthwidget, $this->daytitle, $this->daywidget, $this->yeartitle, $this->yearwidget);
		return $table->getHtml();
	}
	
	public function setyearrange($begin, $end){
		$this->yearwidget->setRange($begin, $end);
	}
	
	public function setsuffix($suffix){
		$this->suffix = $suffix;
	}
	
	public function setprefix($prefix){
		$this->prefix = $prefix;
	}
	
	public function setcaptions(){
		$this->daywidget->setDefaultDisplay( $this->daytitle);
		$this->monthwidget->setDefaultDisplay( $this->monthtitle);
		$this->yearwidget->setDefaultDisplay( $this->yeartitle);
	}
	public function setnames(){
		$this->daywidget->setname  ($this->prefix . $this->dayname   . $this->suffix);
		$this->monthwidget->setname($this->prefix . $this->monthname . $this->suffix);
		$this->yearwidget->setname ($this->prefix . $this->yearname  . $this->suffix);
	}
	
	public function isleap($year){
		if ( $year % 4 == 0 ){
			if ( $year % 100 == 0 ){
				if ( $year % 400  == 0 ){
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}		
	}
}
?>